
package org.openeai.lite.console.actionhandlers;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.openeai.lite.common.control.BrokerController;
import org.openeai.lite.common.exception.OpenEAIException;
import org.openeai.lite.common.util.Constants;


@ManagedBean(name = "brokerConfigTypeActionHandler")
@RequestScoped
public class BrokerConfigTypeActionHandler implements Serializable
{

    private static final long serialVersionUID = 1L;

    public static Logger log = Logger.getLogger(BrokerConfigTypeActionHandler.class);

    private String mqDefaultConfig;

    private String mqPostgresConfig;

    private String mqThroughputConfig;
    
    private String mqServerConfig;

    private String message = null;

    // Set the message
    public String getMessage()
    {
        return message;
    }

    public void setMessage(String aMessage)
    {
        message = aMessage;
    }

    // Default configuration
    public String getMqDefaultConfig()
    {
        try
        {
            if (BrokerController.getInstance().getConfiguration() == Constants.MQ_CONFIG_DEFAULT)
            {
                mqDefaultConfig = "true";
            }
            else
            {
                mqDefaultConfig = "false";
            }
        }
        catch (OpenEAIException e)
        {
            mqDefaultConfig = "false";
        }
        return mqDefaultConfig;
    }

    public void setMqDefaultConfig(String aMqDefaultConfig)
    {
        mqDefaultConfig = aMqDefaultConfig;
    }

    
    // Server configuration
    public String getMqServerConfig()
    {
        try
        {
            if (BrokerController.getInstance().getConfiguration() == Constants.MQ_CONFIG_SERVER)
            {
                mqServerConfig = "true";
            }
            else
            {
                mqServerConfig = "false";
            }
        }
        catch (OpenEAIException e)
        {
            mqServerConfig = "false";
        }
        return mqServerConfig;
    }

    public void setMqServerConfig(String mqServerConfig)
    {
        mqServerConfig = mqServerConfig;
    }
    
 
    // Postgres configuration
    public String getMqPostgresConfig()
    {
        try
        {
            if (BrokerController.getInstance().getConfiguration() == Constants.MQ_CONFIG_POSTGRES)
            {
                mqPostgresConfig = "true";
            }
            else
            {
                mqPostgresConfig = "false";
            }
        }
        catch (OpenEAIException e)
        {
            mqPostgresConfig = "false";
        }
        return mqPostgresConfig;
    }
    public void setMqPostgresConfig(String aMqPostgresConfig)
    {
        mqPostgresConfig = aMqPostgresConfig;
    }

    
    //High-throughput configuration
    public String getMqThroughputConfig()
    {
        try
        {
            if (BrokerController.getInstance().getConfiguration() == Constants.MQ_CONFIG_THROUGHPUT)
            {
                mqThroughputConfig = "true";
            }
            else
            {
                mqThroughputConfig = "false";
            }
        }
        catch (OpenEAIException e)
        {
            mqThroughputConfig = "false";
        }
        return mqThroughputConfig;
    }

    public void setMqThroughputConfig(String aMqThroughputConfig)
    {
        mqThroughputConfig = aMqThroughputConfig;
    }

    
    public void saveConfigType()
    {
        // This sets the configuration type, and accompanying activemq file.
        // Actual reference to config files is stored in openunity.properties,
        // accessed through the Constants class.
        if (mqDefaultConfig.equals("true"))
        {
            BrokerController.getInstance().setConfiguration(Constants.MQ_CONFIG_DEFAULT);
            log.info("Configuration set to " + Constants.MQ_CONFIG_DEFAULT);
            setMessage("Configuration set to " + Constants.MQ_CONFIG_DEFAULT);
        }
        if (mqServerConfig.equals("true"))
        {
            BrokerController.getInstance().setConfiguration(Constants.MQ_CONFIG_SERVER);
            log.info("Configuration set to " + Constants.MQ_CONFIG_SERVER);
            setMessage("Configuration set to " + Constants.MQ_CONFIG_SERVER);
        }
        if (mqPostgresConfig.equals("true"))
        {
            BrokerController.getInstance().setConfiguration(Constants.MQ_CONFIG_POSTGRES);
            log.info("Configuration set to " + Constants.MQ_CONFIG_POSTGRES);
            setMessage("Configuration set to " + Constants.MQ_CONFIG_POSTGRES);
        }
        if (mqThroughputConfig.equals("true"))
        {
            BrokerController.getInstance().setConfiguration(Constants.MQ_CONFIG_THROUGHPUT);            
            log.info("Configuration set to " + Constants.MQ_CONFIG_THROUGHPUT);
            setMessage("Configuration set to " + Constants.MQ_CONFIG_THROUGHPUT);
        }
    }

}
